package com.example.demo.Controller;

import com.example.demo.Controller.vo.xiangqing;
import com.example.demo.Service.TOrderDetailSeivice;
import com.example.demo.Service.TOrderService;
import com.example.demo.Service.TProductService;
import com.example.demo.entity.TOrder;
import com.example.demo.entity.TOrderDetail;
import com.example.demo.entity.TProduct;
import com.example.demo.utils.Result;
import com.example.demo.utils.SnowflakeIdUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@CrossOrigin
@RestController
@RequestMapping("/TOrder")
public class TOrderContorller {
    private TOrderService TO;
    private RabbitTemplate rabbitTemplate;
    @Autowired
    public void setRabbitTemplate(RabbitTemplate rabbitTemplate) {
        this.rabbitTemplate = rabbitTemplate;
    }

    @Autowired
    public void setTO(TOrderService TO) {
        this.TO = TO;
    }
    private TProductService tp;
@Autowired
    public void setTp(TProductService tp) {
        this.tp = tp;
    }
    private TOrderDetailSeivice TC;
@Autowired
    public void setTC(TOrderDetailSeivice TC) {
        this.TC = TC;
    }
    @RequiresPermissions("user:add")
    @RequestMapping("/shengchengdingdan/{userid}")
    public Result shengcheng(@PathVariable Integer userid,
            @RequestBody String[] id){
        Integer[] arr1= new Integer[id.length];
        for (int i = 0; i < id.length; i++) {
            arr1[i]=Integer.parseInt(id[i]);
        }
        List<TProduct> t=tp.chaxun(arr1);
        long l = new SnowflakeIdUtils(20, 20).nextId();
Integer s=0;
        for (int i = 0; i <t.size() ; i++) {
            System.out.println(new TOrderDetail(t.get(i).getId(),l+""));
          Boolean a=TC.zengjia(new TOrderDetail(t.get(i).getId(),l+""));
          s+=t.get(i).getQuantity();
        }
        System.out.println(new TOrder(l+"",s,new Date(),new Date(new Date().getTime() + 180000),1,userid));
        Boolean c =TO.zengjia(new TOrder(l+"",s,new Date(),new Date(new Date().getTime() + 180000),1,userid));
        rabbitTemplate.convertAndSend("spring.boot.delay.exchange",
                "spring.boot.delay.routingkey",l+"");
            return Result.ok().msg("增加成功");
    }
    @RequiresPermissions("user:add")
    @RequestMapping("chaxundingdan/{userid}")
public Result chaxun (@PathVariable Integer userid){
        List<TOrder> order=TO.chaxundingdan(userid);
    return Result.ok().data("order",order);
}
    @RequiresPermissions("user:add")
    @RequestMapping("dingdanxiangqing/{ordernum}")
    public Result dingdanxiangqing (@PathVariable String ordernum){
        List<xiangqing> a=TO.dingdanxiangqing(ordernum);
        return Result.ok().data("order",a);
    }
}
